#include <stdio.h>
#include <stdlib.h>
#include <pic18f6722.h>

#define uint unsigned int
#define uchar unsigned char
#define uchar unsigned char
#define uint unsigned int

#define GAS_GAUGE_ALCC  RC7     //LTC2943 ALCC PIN

void Device_EEPROMInitialize(void);
void AT24C512_writeByte(unsigned char address,unsigned char dataByte);
unsigned char AT24C512_readByte(unsigned int address);
void AT24C512_writeData(unsigned char address,unsigned char numBytes,unsigned char* buf);

extern void PIC_IIC_Delayus();
extern void PIC_IIC_Delayms(unsigned int ms);
extern void PIC_Write_Char_To_EEPROM(uint Addr, uchar Data);
extern uchar PIC_Read_Char_From_EEPROM(uint Addr);
extern void PIC_IIC_Initialize();
extern void PIC_IIC_Start();
extern void PIC_IIC_Stop();
extern void PIC_IIC_SendByte(unsigned char byteData);
extern unsigned char PIC_IIC_ReadByte();
extern unsigned char PIC_IIC_CheckACK();
extern void PIC_IIC_SendACK(unsigned char b_ACK);


//*********************************************************************
/* Initialize EEPROM
 * input void 
 * return: void
 * author: Guichuan.He
 * date: 2016-9-23
*/
//**********************************************************************
void Device_EEPROMInitialize(void)
{

}

//*********************************************************************
/* Write a Byte to EEPROM
 * input: unsigned int address,unsigned char dataByte
 * return: void
 * author: Guichuan.He
 * date: 2016-9-23
*/
//**********************************************************************
void AT24C512_writeByte(unsigned char address,unsigned char dataByte)
{   
  //  unsigned char high_address,low_address;  
//	high_address = (address>>8) & 0x00ff;
//	low_address = address & 0xff;

	PIC_IIC_Start();   
    PIC_IIC_SendByte(0xa0);
    PIC_IIC_CheckACK();       
  //  PIC_IIC_SendByte(high_address);       
  //  PIC_IIC_CheckACK();   
    PIC_IIC_SendByte(address);    
    PIC_IIC_CheckACK();   
    PIC_IIC_SendByte(dataByte); 
    PIC_IIC_CheckACK();   
    PIC_IIC_Stop();   
    PIC_IIC_Delayms(20);    
  
}   

//*********************************************************************
/* Read a Byte from EEPROM
 * input: unsigned int address
 * return: unsigned char
 * author: Guichuan.He
 * date: 2016-9-23
*/
//**********************************************************************
unsigned char AT24C512_readByte(unsigned int address)
{
    unsigned char ch;
  //  unsigned char high_address,low_address;  
//	high_address = (address>>8) & 0x00ff;
//	low_address = address & 0xff;

	PIC_IIC_Start();                    
    PIC_IIC_SendByte(0xa0);        
    PIC_IIC_CheckACK();               
  //  PIC_IIC_SendByte(high_address);    
  //  PIC_IIC_CheckACK();                 
    PIC_IIC_SendByte(address);    
    PIC_IIC_CheckACK();                  
    PIC_IIC_Start();                 
    PIC_IIC_SendByte(0xa1);         
    PIC_IIC_CheckACK();             
 
    ch = PIC_IIC_ReadByte();  
    PIC_IIC_SendACK(0);  
   
    PIC_IIC_Stop(); 
    return  ch;
}

//*********************************************************************
/* Write string or data to EEPROM
 * input: unsigned char address,unsigned char numBytes,unsigned char* buf
 * return: void
 * author: Guichuan.He
 * date: 2016-9-23
*/
//**********************************************************************
void AT24C512_writeData(unsigned char address,unsigned char numBytes,unsigned char* buf)
{   
    while(numBytes--)   
    {   
        AT24C512_writeByte(address++,*buf++);   
    }   
}   
//*********************************************************************************  
void AT24C512_readData(unsigned char beginAddr,unsigned char dataSize,unsigned char* buf)//????????   
{   
    PIC_IIC_Start();                     
    PIC_IIC_SendByte(0xa2);        
    PIC_IIC_CheckACK();            
    PIC_IIC_SendByte(beginAddr);      
    PIC_IIC_CheckACK();                 
    PIC_IIC_Start();                
    PIC_IIC_SendByte(0xa3);        
    PIC_IIC_CheckACK();              
    while(dataSize--)                
    {   
        *buf++ = PIC_IIC_ReadByte(); 
        PIC_IIC_SendACK(dataSize);  
    }   
    PIC_IIC_Stop();                     
}   
//*************************************************************************************  

